package cn.com.coding.app;

import cn.com.coding.common.lang.ResT;
import cn.com.coding.common.utils.PageUtil;
import cn.com.coding.system.domin.ProductInfoEntity;
import cn.com.coding.system.domin.UserCardProductEntity;
import cn.com.coding.system.domin.dto.UserCardProductDto;
import cn.com.coding.system.service.ProductInfoService;
import cn.com.coding.system.service.UserCardProductService;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 购物车详情
 *
 * @author mxz
 */
@RestController
@RequestMapping("cat/userCardProduct")
public class UserCardProductController {

    @Resource
    private UserCardProductService userCardProductService;

    @Resource
    private ProductInfoService productInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public ResT list(@RequestParam Map<String, Object> params) {
        PageUtil page = userCardProductService.queryPage(params);

        return ResT.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{pid}")
    public ResT info(@PathVariable("pid") String pid) {
        UserCardProductEntity userCardGood = userCardProductService.getById(pid);

        return ResT.ok().put("userCardGood", userCardGood);
    }


    /**
     * 获取用户购物车 待优化
     *
     * @param userPid
     * @return
     */
    @RequestMapping("/getShoppingCart/{userPid}")
    public ResT getOneCardGoods(@PathVariable("userPid") String userPid) {
        List<UserCardProductDto> shoppingCartData = userCardProductService.getCardProductByUserPid(userPid);

        return ResT.ok().setData(MapUtil.builder().put("shoppingCartData", shoppingCartData).build());
    }

    /**
     * 修改数量
     */
    @RequestMapping("/updateShoppingCart")
    public ResT updateShoppingCart(@RequestBody UserCardProductDto userCardProductDto) {
        UserCardProductEntity userCardGoodEntity = userCardProductService.getOne(new QueryWrapper<UserCardProductEntity>().eq("card_pid", userCardProductDto.getCardPid()).eq("product_pid", userCardProductDto.getProductPid()));

        ProductInfoEntity good = productInfoService.getById(userCardProductDto.getProductPid());

        userCardGoodEntity.setQuanity(userCardProductDto.getQuanity());
        userCardGoodEntity.setPrice(good.getPrice().multiply(BigDecimal.valueOf(userCardProductDto.getQuanity())));
        userCardProductService.updateById(userCardGoodEntity);

        return ResT.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public ResT save(@RequestBody UserCardProductEntity userCardGood) {
        userCardProductService.save(userCardGood);

        return ResT.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public ResT update(@RequestBody UserCardProductEntity userCardGood) {
        userCardProductService.updateById(userCardGood);

        return ResT.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ResT delete(@RequestBody String[] pids) {
        userCardProductService.removeByIds(Arrays.asList(pids));

        return ResT.ok();
    }

    /**
     * 用户删除购物车商品
     */
    @RequestMapping("/deleteShoppingCart")
    public ResT deleteGood(@RequestBody Map<String, String> map) {

        userCardProductService.deleteProduct(map.get("userPid"), map.get("productPid"));

        return ResT.ok();
    }

}
